/*
 * RMFacadeMonitor.java    ver   27/nov/2009
 *
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transactions.management.rm;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.mentalsmash.tazio.commons.timer.TimingsLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

/**
 * Implementation of the {@link RMFacadeMonitorMBean}
 * 
 * @version 0.0.1 27/nov/2009
 * @author Andrea Reale <andrea.reale@gmail.com>
 * 
 */
class RMFacadeMonitor implements RMFacadeMonitorMBean {

    private final static Logger log = LoggerFactory
	    .getLogger(RMFacadeMonitor.class);
    private final RMTransactionsFacadeImpl toMonitor;
    // the timings log keeps the timings of calls of method annotated with
    // @MonitorTimingPerformances
    private final TimingsLog timeLog;

    @Inject
    RMFacadeMonitor(final RMTransactionsFacadeImpl toMonitor,
	    final TimingsLog tlog) {
	this.toMonitor = toMonitor;
	this.timeLog = tlog;
    }

    @Inject
    public void register(final MBeanServer server) {
	try {
	    final ObjectName name = new ObjectName(
		    "org.mentalsmash.tazio.transactions", "type",
		    "RMFacadeMonitor");
	    server.registerMBean(this, name);
	} catch (final JMException e) {
	    log.error("Unable to register MBean", e);
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getNumBeginRequests()
     */
    @Override
    public int getNumBeginRequests() {
	return toMonitor.getNumBeginRequests();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getNumCommitRequests()
     */
    @Override
    public int getNumCommitRequests() {
	return toMonitor.getNumCommitRequests();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getNumGetRootSetRequests()
     */
    @Override
    public int getNumGetRootSetRequests() {
	return toMonitor.getNumRSRequests();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getNumWATTRequests()
     */
    @Override
    public int getNumWATTRequests() {
	return toMonitor.getNumWattRequests();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getNumUnknownWATTResponses()
     */
    @Override
    public int getNumUnknownWattResponses() {
	return toMonitor.getNumUnknownWatt();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getAvgCommitRequestDuration()
     */
    @Override
    public double getAvgCommitRequestDuration() {
	final String key = RMTransactionsFacadeImpl.class.getName() + "#"
		+ "commitTransaction";
	return timeLog.getAvgMeasure(key);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getMaxCommitRequestDuration()
     */
    @Override
    public long getMaxCommitRequestDuration() {
	final String key = RMTransactionsFacadeImpl.class.getName() + "#"
		+ "commitTransaction";

	return timeLog.getMaxMeasure(key);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getMinCommitRequestDuration()
     */
    @Override
    public long getMinCommitRequestDuration() {
	final String key = RMTransactionsFacadeImpl.class.getName() + "#"
		+ "commitTransaction";

	return timeLog.getMinMeasure(key);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.management.rm.RMFacadeMonitorMBean
     * #getStdDeviationCommitRequest()
     */
    @Override
    public double getStdDeviationCommitRequest() {
	final String key = RMTransactionsFacadeImpl.class.getName() + "#"
		+ "commitTransaction";
	return timeLog.getStdDeviation(key);
    }

}
